#include "unp.h"	
#include "msg_handlers.c"
#include "globals.h"

char* getHostAddress(int vm);

int 
main(int argc , char ** argv)
{
	int sockfd,sel, vm_machine, max_fdp, source_port, count = 0, i, recvd_vm; 
	struct sockaddr_un cliaddr;
	char serveraddr[IPSIZE];
	struct timeval five_sec;
	struct hostent *host;
	fd_set rset;
	char host_name[50];
	char client_message[] = "I need time\0";
	char server_message[50];
	char template_file[] = "/client.tmp\0";	
	char IP_recvd[17];
	char vm_name[10] = "vm6";
	sockfd = Socket(AF_LOCAL, SOCK_DGRAM, 0);
	bzero(&cliaddr, sizeof(cliaddr));
	cliaddr.sun_family = AF_LOCAL;
	mkstemp(template_file);
	strcpy(cliaddr.sun_path, template_file);
	unlink(cliaddr.sun_path);
	
	Bind( sockfd, (SA *) &cliaddr, sizeof(cliaddr)); 
	FD_ZERO(&rset);

	while(1){
		char vm_machine_str[2] = "\0";
		printf("\n Please enter the server you want to connect to \n");
		scanf("%d",&vm_machine);
		if (vm_machine < 1 || vm_machine >10) {
			printf("Enter valid server number\n");
			continue;
		}
		
	//	snprintf(vm_machine_str, vm_machine, "%d", 2);
		//printf("The machine string is %s", vm_machine_str);
		//strcat(vm_name, vm_machine_str);
		//printf("The machine string is %s", vm_machine);
repeat_send:	//printf("\n Sending from %s to vm%d \n", host_name, vm_machine);
		strcpy(host_name, getHostAddress(vm_machine));
		printf("Client at node %s sending request to server at vm%d\n",host_name, vm_machine);
		if (count)
			printf("Forced route discovery bit set\n");
		msg_send(sockfd, host_name, 0, client_message, count);
		five_sec.tv_sec = 10;
		five_sec.tv_usec = 0;
    		FD_SET(sockfd,&rset);
		max_fdp = sockfd + 1;
		sel = select( max_fdp, &rset,NULL,NULL,&five_sec);	 
		if(FD_ISSET(sockfd,&rset)){
			msg_recv(sockfd, server_message, IP_recvd, &source_port);
		//	recvd_vm = get_vm_name(IP_recvd);
			printf(	"\n Client at node %s: recieved from vm%d \n", host_name, recvd_vm);
			printf("\nTime: %s\n",server_message);
		}
		else if (sel == 0)
		{
			if( count < 1 ){
				count++;
				printf("\n Client at node %s: Time out on response from vm%d\n", host_name, vm_machine);
				goto repeat_send;
			}
			else {
				printf("\n Client at node %s: Time out on response from vm%d\n", host_name, vm_machine);
				continue;
			}
		}
		count = 0;
	} 	
	unlink(cliaddr.sun_path);
}

char* getHostAddress(int vm) {
	struct hostent *hp = NULL;
	struct in_addr inetaddr;
	struct in_addr **pptr;
	char* connectingIPAddress = NULL;
	int i;
	
	char vmName[20];
	sprintf(vmName, "vm%d", vm);
	
	if((hp = gethostbyname(vmName)) == NULL) {
		err_quit("Error: Could Not Get Host Information for VM%d", vm);
	} else {
		pptr = (struct in_addr **) hp->h_addr_list;

		for(i = 0; pptr[i] != NULL; i++) { 
      		char straddr[INET_ADDRSTRLEN];
      		if(inet_ntop(AF_INET, pptr[i], straddr, sizeof(straddr)) != NULL) {
          		if(connectingIPAddress == NULL) {
              		connectingIPAddress = straddr;
					return connectingIPAddress;
          		}
      		} else {
              	err_quit("inet_ntop: Error Retrieving IP Addresses for Host (%d)\n", vmName);
      		}
		}
		printf("\n");
	}
	return "";
}
